package com.niit.bc.dao.impl;

import com.niit.bc.util.web.PropertiesUtil;

import java.sql.*;

public class BaseDao {

    public Connection getConnection() {
        Connection conn = null;

        PropertiesUtil proUtil = new PropertiesUtil();
        String DriverClassName = proUtil.getPropertie("jdbc.driverClassName");
        String url = proUtil.getPropertie("jdbc.url");
        String DBuserName = proUtil.getPropertie("jdbc.userName");
        String DBuserpwd = proUtil.getPropertie("jdbc.password");

        try {
            Class.forName(DriverClassName);
            conn = DriverManager.getConnection(url, DBuserName, DBuserpwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public void closeSource(ResultSet rs, Statement st, PreparedStatement ps, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (st != null) {
            try {
                st.close();
                st = null;
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (ps != null) {
            try {
                ps.close();
                ps = null;
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
                conn = null;
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
